﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using maranov.SymlinkManager.ApplicationPrivilege;
using maranov.SymlinkManager.FilesystemLinks;

namespace maranov.SymlinkManager
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            TestPrivileges();
            InitializeComponent();
        }

        /// <summary>
        /// Tests application process for sufficient privileges.
        /// Restarts application as the administrator if neccessary.
        /// </summary>
        private void TestPrivileges()
        {
            IApplicationPrivilegeElevator linkPrivilegeChecker =
                new ApplicationPrivilegeElevator(
                    new ApplicationPrivilegeCheckProvider().PrivilegeCheckRoutines);
            try
            {
                linkPrivilegeChecker.CheckAndElevate(
                    new ApplicationTerminator(delegate()
                    {
                        Close();
                    })
                    );
            }
            catch (ApplicationElevationException e)
            {
                if (e.IsAlreadyAdmin)
                {
                    // Privilege error while already Administrator
                    //TODO: Show error.
                    Close();
                }
                else
                {
                    // Probably a closed UAC prompt
                    //TODO: Show error?
                    Close();
                }
            }
        }
    }
}
